<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\libs\Util;
use app\model\goods\GoodsFreightModel;
use app\model\goods\GoodsModel;
use app\model\goods\GoodsTypeModel;
use think\Request;
use app\factory\GoodsType as GoodsTypeFactory;

/**
 * GoodsFreight 商品运费模板管理
 * Class GoodsFreight
 * @package app\controller
 */
class GoodsFreight extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new GoodsFreightModel();
    }

    /**
     * 分页列表
     * @param string $keyword
     * @param int $page
     * @param int $limit
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPage($keyword = '', $page = 1, $limit = 10)
    {
        $data = $this->model->getPageData([], $keyword, $page, $limit);
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 获取单条数据
     * @param string $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo($id = '')
    {
        $data = $this->model->getInfo($id);
        return self::returnMsg(1, $data);
    }

    /**
     * 保存
     * @param Request $request
     * @return \think\response\Json
     */
    public function onSave(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'name' => $request->param('name'),
            'base_freight' => $request->param('base_freight'),
            'one_freight' => $request->param('one_freight'),
            'default' => $request->param('default')
        ];
        if (empty($formData['id'])) {
            $validate = 'app\validate\GoodsFreightValidate.add';
        } else {
            $validate = 'app\validate\GoodsFreightValidate';
        }
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            if (intval($formData['default']) == 1) {
                $this->model->where(['store_id' => $this->currentUser['store_id']])->update(['default' => 0]);
            }
            $ret = $this->model->saveData($formData, false, false);//修改是否添加进缓存。。。
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function onDelete($id)
    {
        $flag = $this->model::destroy(['id' => $id]);
        if (false !== $flag) {
            $this->model->delRedis($id);
            $this->model->redis->zRem($this->model->redis_key . ':rank', $id);
            $this->model->addToRecycle($id, 'GoodsFreightModel', '商品搜索关键词管理', $this->currentUser['user_id'], $this->currentUser['user_type'], 'goods');
            return self::returnMsg(1);
        } else {
            return self::returnMsg(1002);
        }
    }

    /**
     * 设置默认
     * @param Request $request
     * @return \think\response\Json
     */
    public function onDefault(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'default' => $request->param('default'),
        ];
        $validate = 'app\validate\GoodsFreightValidate.updefault';
        $result = $this->validate($formData, $validate);
        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            if (intval($formData['default']) == 1) {
                $this->model->where('store_id', $this->currentUser['store_id'])->update(['default' => 0]);
            }
            $ret = $this->model->saveData($formData, false, false);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

}
